<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/7/4
 * Time: 23:25
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\BankTraits\YINBAOBankMap;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class YINBAOPayment extends RechargeBaseAbstract implements WAPable
{
    use YINBAOBankMap;

    protected $gw_pay = 'http://pay.an216.com/interface/chargebank.aspx';
    protected $gw_query = 'http://pay.an216.com/pay/Query.aspx';


    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['restate'] == 0) return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr = "";
        $signStr .= "orderid=" . $data['orderid'] . '&';
        $signStr .= "restate=" . $data['restate'] . '&';
        $signStr .= "ovalue=" . $data['ovalue'];
        $signStr .= $this->getMchKey();
        $sign = md5($signStr);
        if (strcasecmp($sign, $data['sign']) == 0) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['orderid'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['ovalue'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::YINBAO;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::ALI => 'ALIPAY',
            PaymentMap::WX => 'WEIXIN',
            PaymentMap::WX_WAP => 'WXWAP',
            PaymentMap::ALI_WAP => 'ALIWAP',
            PaymentMap::QQ => 'QQCODE',
            PaymentMap::QQ_WAP => 'QQWAP',
            PaymentMap::JD => 'QQWAP',
            PaymentMap::JD_WAP => 'JDWAP',
            PaymentMap::BANK_SCAN => 'VISA',
            PaymentMap::BANK_WAP => 'BANKWAP'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('type', 1);
        $this->setParameter('parter', $this->getMchId());
        $this->setParameter('sign', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            parse_str($res['body'], $output);
            if (@$output['ordstate'] == 1) return true;
            return $output['ordstate'] == 0 ? '处理中' : '失败';
        }
        return $res['body'];

    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() .'?' . http_build_query($this->getParameters());
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() .'?' . http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr = "";
        $signStr .= "parter=" . $this->getParameter('parter') . '&';
        $signStr .= "type=" . $this->getParameter('type') . '&';
        $signStr .= "orderid=" . $this->getParameter('orderid') . '&';
        $signStr .= "callbackurl=" . $this->getParameter('callbackurl');
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = "";
        $signStr .= "parter=" . $this->getParameter('parter') . '&';
        $signStr .= "type=" . $this->getParameter('type') . '&';
        $signStr .= "orderid=" . $this->getParameter('orderid') . '&';
        $signStr .= "key=" . $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'ok';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('value', $rechargeOrder->order_amt);
        $this->setParameter('parter', $this->getMchId());
        if ($rechargeOrder->order_data['recharge_type'] == PaymentMap::BANK) {
            $type = $this->getBank($rechargeOrder->order_data['bank_code']);
        } else {
            $type = $this->getPaymentMap($rechargeOrder->order_data['recharge_type']);
        }
        $this->setParameter('type', $type);
        $this->setParameter('callbackurl', $this->getCallbackUrl());
        $this->setParameter('hrefbackurl', $this->getReturnUrl());
        $this->setParameter('sign', $this->paySign());

    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() .'?' . http_build_query($this->getParameters()));
    }
}